package com.bolovan.infovaluta.reader;

import java.io.IOException;
import java.math.BigDecimal;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.bolovan.infovaluta.bean.ExchangeRate;
import com.bolovan.infovaluta.bean.ExchangeRateEntry;
import com.bolovan.infovaluta.bean.ExchangeRateSeller;
import com.bolovan.infovaluta.util.RateMaps;
import com.bolovan.infovaluta.util.RateUtil;

public class IngReader extends ExchangeReader {

	public IngReader(ExchangeRateSeller seller) {
		super(seller);
	}

	@Override
	public void buildRateMap(ExchangeRate map) throws IOException {
		fetchING(map);
	}

	private void fetchING(ExchangeRate map) throws IOException {
		setParsingDate(RateMaps.date);
		Document doc = RateUtil.getDocument(ExchangeRateSeller.ING.getUrl());
		Elements elements = doc.select("#wrapper").first().select(".tabbertab ").last().select(".liner");

		// first one is the header
		for (int i = 1; i < elements.size(); i++) {
			Elements liners = elements.get(i).select("div.leftsider");
			String currency = liners.get(0).text().trim();

			BigDecimal ask = new BigDecimal(liners.get(1).text().replace(String.valueOf((char) 160), " ").trim());
			BigDecimal bid = new BigDecimal(liners.get(2).text().replace(String.valueOf((char) 160), " ").trim());
			map.addToMap(new ExchangeRateEntry(currency, bid, ask));

		}

	}
}
